package com.xhtt.modules.app.controller;


import com.xhtt.common.utils.R;
import com.xhtt.common.utils.WordUtils;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * APP测试接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:47
 */
@RestController
@RequestMapping("/app")
@Api("APP测试接口")
public class AppTestController {

    @Login
    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser SysUserEntity user) {
        return R.ok().put("user", user);
    }

    @Login
    @GetMapping("userId")
    @ApiOperation("获取用户ID")
    public R userInfo(@RequestAttribute("userId") Integer userId) {
        return R.ok().put("userId", userId);
    }

    @GetMapping("notToken")
    @ApiOperation("忽略Token验证测试")
    public R notToken() {
        return R.ok().put("msg", "无需token也能访问。。。");
    }


    @GetMapping("/exportSellPlan")
    public void exportSellPlan(HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> params) {
        Calendar calendar = Calendar.getInstance();// 取当前日期。
        //获得数据
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("time", "2018-10-22");
        map.put("company", "");
        map.put("address", "江苏常州");
        map.put("contant", "内容");

        AppTestEntity info1 = new AppTestEntity(1, "小王", "经理", "研发部");
        AppTestEntity info2 = new AppTestEntity(2, "小王", "经理", "研发部");
        AppTestEntity info3 = new AppTestEntity(3, "小王", "经理", "研发部");
        AppTestEntity info4 = new AppTestEntity(4, "小王", "经理", "研发部");
        AppTestEntity info5 = new AppTestEntity(5, "小王", "经理", "研发部");
        List<AppTestEntity> infos = Arrays.asList(info1, info2, info3, info4, info5);
        map.put("infos", infos != null ? infos : "");
        /*List<String> list = Arrays.asList("小王", null, "小李", "小赵", "王五");
        int i = 1;
        for (String name : list) {
            map.put("name" + i, name != null ? name : "");
            i++;
        }*/
        try {
            WordUtils.exportWord(request, response, map, "firstMeetingPersonnel.ftl", "测试");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
